package com.zdc.learn.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * nio  client
 */
public class NioClient {

    private SocketChannel socketChannel = null;

    private Selector selector = null;

    public NioClient(){
        try {
            //链接服务
            socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 6666));
            //获取selector
            selector = Selector.open();
            //非阻塞
            socketChannel.configureBlocking(false);

            //将channel 注册到 selector
            socketChannel.register(selector, SelectionKey.OP_READ);
            //得到username
            String username = socketChannel.getLocalAddress().toString();

            System.out.println(username + " is ok");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void getMsg(){
        try{
            int count = selector.select();
                //判断是否有可用的通道
                if (count >0){
                    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                    while (iterator.hasNext()){
                        SelectionKey key = iterator.next();
                        if (key.isReadable()){
                            SocketChannel sc = (SocketChannel)key.channel();
                            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                            //从管道中读取数据
                            sc.read(byteBuffer);
                            String msg = new String(byteBuffer.array());
                            System.out.println(msg);

                        }
                    }
                    iterator.remove(); //删除当前的selectionKey, 防止重复操作
                }

        }catch (Exception e){
            e.printStackTrace();
        }



    }
    //发送数据
    public void sendMsg(String msg){
        try {
            socketChannel.write(ByteBuffer.wrap(msg.getBytes()));
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        NioClient nioClient = new NioClient();
        new Thread(()->{
            while (true){
                nioClient.getMsg();
                try {
                    Thread.currentThread().sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            String msg = s;
            nioClient.sendMsg(msg);
        }

    }
}
